﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;

namespace Sec_scan安全漏洞扫描系统_2._0.Services
{
    /// <summary>
    /// 获取系统信息
    /// </summary>
    public class Tl_SystemInfo
    {
        // 获取基带内存信息
        public static string GetPhysicalMemoryInfo()
        {
            try
            {
                ulong totalMemory = 0;
                ulong availableMemory = 0;

                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Capacity FROM Win32_PhysicalMemory"))
                {
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        totalMemory += Convert.ToUInt64(obj["Capacity"]);
                    }
                }

                // 获取可用内存
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT FreePhysicalMemory FROM Win32_OperatingSystem"))
                {
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        availableMemory = Convert.ToUInt64(obj["FreePhysicalMemory"]) * 1024; // 转换为字节
                    }
                }

                return $"{totalMemory / (1024 * 1024 * 1024)} GB ({availableMemory / (1024 * 1024 * 1024)} GB 可用)";
            }
            catch (Exception ex)
            {
                return $"获取内存信息时出错: {ex.Message}";
            }
        }

        // 获取处理器信息
        public static string GetProcessorInfo()
        {
            try
            {
                string processorInfo = string.Empty;
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name, MaxClockSpeed FROM Win32_Processor"))
                {
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        string name = obj["Name"]?.ToString();
                        int maxClockSpeed = Convert.ToInt32(obj["MaxClockSpeed"]);
                        processorInfo += $"{name} {maxClockSpeed / 1000.0} GHz\n";
                    }
                }
                return processorInfo.Trim();
            }
            catch (Exception ex)
            {
                return $"获取处理器信息时出错: {ex.Message}";
            }
        }

        // 获取设备ID信息
        public static string GetDeviceID()
        {
            try
            {
                string deviceID = string.Empty;
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct"))
                {
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        deviceID = obj["UUID"]?.ToString();
                    }
                }
                return string.IsNullOrEmpty(deviceID) ? "未找到设备 ID" : deviceID;
            }
            catch (Exception ex)
            {
                return $"获取设备 ID 时出错: {ex.Message}";
            }
        }
    }
}
